package com.zhaoxi.project.wms.service;

import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.project.wms.mapper.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

//样例
@Service
public class StockService extends BaseService {
  @Autowired
  private StockMapper stockMapper;

  @Autowired
  private OrderService orderService;

  //查询数据集合
  public List<PageData> getList(PageData pd) {
    return stockMapper.getList(pd);
  }

  //查询数据对象 通过id
  public PageData getById(PageData pd) {
    PageData pd1 = new PageData();
    if (pd.getLong("id") == null) {
      return null;
    }
    pd1.put("id", pd.getLong("id"));
    List<PageData> list = stockMapper.getList(pd1);
    if (list.size() > 0) {
      return list.get(0);
    }
    return null;
  }


  //查询数据行数
  public PageData getCount(PageData pd) {

    return this.success(stockMapper.getCount(pd));

  }

  /**
   * 要判断是否订单是出库还是入库，是否同一个批次号，如果是同一个批次号，在数量够的情况下，优先更新数据。不够在从其它批次号处理
   */
  @Transactional(propagation = Propagation.REQUIRES_NEW)
  public PageData editForOrderExec(PageData pd) {
    PageData order = orderService.getById(pd);
    if (order == null) {
      return this.error("没有查询到该单据的id");
    }
    if (!order.getString("state").equals("5")) {
      return this.error("单据状态并不是待执行！！！");
    }
    PageData orderType = orderService.getTypeById(order);
    if (!(orderType.getString("stockType").equals("out") || orderType.getString("stockType").equals("in") || orderType.getString("stockType").equals("nothing"))) {
      return this.error("单据类别有误，请确认！！！");
    }
    List<PageData> orderLineList = orderService.getLineList(pd);
//    先处理库存相关的事务
    for (PageData line : orderLineList) {
      line.put("warehouseId", order.getLong("selfWhId"));
      if (orderType.getString("stockType").equals("in")) {
        PageData res = this.stockIn(line);
        if (res.isError()) {
          TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
          return res;
        }
      } else if (orderType.getString("stockType").equals("out")) {
        PageData res = this.stockOut(line);
        if (res.isError()) {
          TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
          return res;
        }
      }
    }
//      后续还有单据要生成的，暂时没写！！！
    if (orderType.isEmpty("nextCreateType")) {
    }

    pd.put("userExecId", this.getUserId());
    pd.put("updateBy", this.getUsername());
    pd.put("state", "3");
    orderService.edit(pd);
    return this.success("成功");
  }

  //库存入库，不对外开发
  @Transactional(propagation = Propagation.REQUIRED)
  protected PageData stockIn(PageData pd) {
    PageData pd1 = new PageData();
    pd1.put("productId", pd.getLong("productId"));
    pd1.put("batchCode", pd.getString("batchCode"));
    pd1.put("warehouseId", pd.getLong("warehouseId"));
    List<PageData> list = stockMapper.getList(pd1);

    if (pd.getDouble("quantity") > 0) {
      pd1.put("quantity", pd.getDouble("quantity"));
    } else {
      this.error("入库数量不能小于零！！！");
    }
    pd1.put("areaId", pd.getLong("areaId"));
    pd1.put("measureUnit", pd.getString("measureUnit"));
    int row = 0;
    if (list.size() > 0) {
      pd1.put("id", list.get(0).getLong("id"));
      row = stockMapper.edit(pd1);
    } else {
      row = stockMapper.add(pd1);
    }
    return this.responses(row);
  }

  //库存出库，不对外开发
  @Transactional(propagation = Propagation.REQUIRED)
  protected PageData stockOut(PageData pd) {
    PageData pd1 = new PageData();
    pd1.put("productId", pd.getLong("productId"));
    pd1.put("batchCode", pd.getString("batchCode"));
    pd1.put("warehouseId", pd.getLong("warehouseId"));
    pd1.put("quantityHas", 0);//大于0的数据
    pd1.put("sortExpireDate", 0);//按照过期时间处理
    List<PageData> list = stockMapper.getList(pd1);
    double quantity, quantityStock;
    quantity = pd.getDouble("quantity");
    if (quantity > 0) {
      quantity = quantity * -1;
    } else {
      this.error("出库数量不能小于零！！！");
    }
    pd1.put("areaId", pd.getLong("areaId"));
    pd1.put("measureUnit", pd.getString("measureUnit"));
    for (PageData k : list) {
      pd1.put("id", k.getLong("id"));
      quantityStock = k.getDouble("quantity");
      if (quantityStock + quantity >= 0) {
        stockMapper.edit(pd1.put("quantity", quantity));
        quantity = quantityStock + quantity;
        return this.success("当前行扣库存完成");
      } else {
        stockMapper.edit(pd1.put("quantity", quantityStock * -1));
        quantity = quantityStock + quantity;
      }
    }
    if (quantity < 0) {
      return this.error("库存不足1！！！");
    }
    return this.success("当前行扣库存完成");
  }

  //删除
  public PageData del(Long id) {
    return this.responses(stockMapper.del(id));
  }


  //查询数据集合
  public PageData getLogList(PageData pd) {

    List<PageData> list = stockMapper.getLogList(pd);
    return this.responses(list);

  }

  //查询数据行数
  public PageData getLogCount(PageData pd) {

    return this.success(stockMapper.getLogCount(pd));

  }

  //删除
  public PageData addLog(PageData pd) {

    return this.responses(stockMapper.addLog(pd));

  }
}
